
#include "CCamera.h"
//#include "CTileManager.h"
#include "SGD Wrappers\CSGD_DirectInput.h"
#include "CGame.h"

CCamera::CCamera()
{
	SetPosX(0.0f);
	SetPosY(0.0f);
	SetWidth(800.0f);
	SetHeight(600.0f);
	m_fSpeed = 6.0f;
	m_fOffset = 0.0f;
	index = 0;
	m_bPlayerMode = true;
	m_bFoundPoint = false;
}

CCamera::~CCamera()
{
	waterspawns.clear();
}

void CCamera::Update()
{
	if(m_bPlayerMode)
	{
		//if(m_pPlayer->GetPosX() >= GetPosX()+m_fWidth-RIGHT)
		//	SetPosX(GetPosX() + m_fSpeed);
		//if(m_pPlayer->GetPosY()-100 <= GetPosY()+TOP)
		//	SetPosY(GetPosY() - m_fSpeed);
		//if(m_pPlayer->GetPosX() <= GetPosX()+LEFT)
		//	SetPosX(GetPosX() - m_fSpeed);
		//if(m_pPlayer->GetPosY() >= GetPosY()+m_fHeight-BOTTOM)
		//	SetPosY(GetPosY() + m_fSpeed);
		//if(m_bPanning)
		//{
		//	SetPosY(GetPosY() + m_fOffset);
		//	if(m_fOffset < 0)
		//	{
		//		if(GetPosY()+m_fHeight <= m_pPlayer->GetPosY()-100)
		//			SetPosY(m_pPlayer->GetPosY()-m_fHeight-100);
		//	}
		//	else
		//	{
		//		if(GetPosY() >= m_pPlayer->GetPosY()-100)
		//			SetPosY(m_pPlayer->GetPosY()-100);
		//	}
		//	
		//}
		//else
		//	m_fOffset = 0.0f;
	}
	else
	{
		if(waterspawns.size() > 0)
		{
			if(GoToPoint(waterspawns[index]))
			{
				index++;
				if(index == waterspawns.size())
					m_bPlayerMode = true;
			}
		}
		else
			m_bPlayerMode = true;
	}
	//if(m_pPlayer->IsDashing())
	//	m_fSpeed = 20.0f;
	//else
		m_fSpeed = 6.0f;

	if(GetPosX() <= 0)
		SetPosX(0.0f);
	if(GetPosY() <= 0)
		SetPosY(0.0f);/*
	if(GetPosX()+m_fWidth >= CTileManager::GetInstance()->GetMapWidth()*32)
		SetPosX(CTileManager::GetInstance()->GetMapWidth()*32-m_fWidth);
	if(GetPosY()+m_fHeight >= CTileManager::GetInstance()->GetMapHeight()*32)
		SetPosY(CTileManager::GetInstance()->GetMapHeight()*32-m_fHeight);*/
}

CCamera* CCamera::GetInstance()
{
	static CCamera instance;
	return &instance;
}

void CCamera::ResetCamera()
{
	//SetPosX(m_pPlayer->GetPosX()-300);
	//SetPosY(m_pPlayer->GetPosY()-200);
	index = 0;
	waterspawns.clear();
}

bool CCamera::IsOnCamera(float nPosX, float nPosY)
{
	if(nPosX > GetPosX() && nPosY > GetPosY() && nPosX < (GetPosX()+m_fWidth) && nPosY < (GetPosY()+m_fHeight))
		return true;
	else
		return false;
}

//void CCamera::PanCameraUp()
//{
//	m_bPanning = true;
//	m_fOffset -= PANRATE;
//	if(m_fOffset <= -m_fHeight*0.005f)
//		m_fOffset = -m_fHeight*0.005f;
//}
//
//void CCamera::PanCameraDown()
//{
//	m_bPanning = true;
//	m_fOffset += PANRATE;
//	if(m_fOffset >= m_fHeight*0.005f)
//		m_fOffset = m_fHeight*0.005f;
//}

bool CCamera::GoToPoint(POINT p)
{
	if(p.x >= GetPosX()+m_fWidth)
	{
		SetPosX(GetPosX() + 2.0f);
		m_bFoundPoint = false;
	}
	if(p.y <= GetPosY())
	{
		SetPosY(GetPosY() - 2.0f);
		m_bFoundPoint = true;
	}
	if(p.x <= GetPosX())
	{
		SetPosX(GetPosX() - 2.0f);
		m_bFoundPoint = true;
	}
	if(p.y >= GetPosY()+m_fHeight)
	{
		SetPosY(GetPosY() + 2.0f);
		m_bFoundPoint = false;
	}
	return m_bFoundPoint;
}

void CCamera::AddPoint(POINT p)
{
	waterspawns.push_back(p);
}
